
//THIS CODE FILE PRODUCES FIGURE A.6 AND FIGURE A.7

global main "[YOUR PATH HERE]"
global output "${main}/output"
global mwdata "${main}/data/mw"


//enrollment type switches
local switch_all = 0
local switch_nponly  = 0
local switch_fponly  = 0
	local switch_ftonly  = 0
	local switch_ptonly  = 0
	local switch_pubonly = 1
	local switch_menonly = 0
	local switch_womenonly = 0
	local switch_msu = 0
	local switch_urban = 0
	
local switch_controls = 0 //adds controls for free college (dummies) and for the unemployment rate

	
// one of these switches must be on!!
	//indicates whether we want to use percent change as our measure of MW change size 
	//or whether we want to use a nominal change (50 cent, 75 cents, etc.)
local switch_nominal    = 0
local switch_pctchng    = 1
local switch_pctHSwage  = 0

if `switch_nominal'{
	local thresholds .25 .5 .75
	local xtitle "minimum wage change"
	local measure nom
}
else if `switch_pctchng'{
	local thresholds .06 .08 .101  //.12
	local xtitle "percent change to minimum wage"
	local measure pct
}
else if `switch_pctHSwage'{
	local thresholds .02 .04 .06
	local xtitle "minimum wage change as a percent of average high school grad wages"
	local measure pctHS
}
	
//create results vectors 
matrix col1 = J(8,2,.)
matrix col2 = J(8,2,.)
matrix col3 = J(8,3,.)
//matrix col4 = J(8,3,.)
matrix results = J(8,9,.)

local n = 1

********************************************************************************
**CREATE STACKED DATA AT EACH LEVEL**
********************************************************************************
foreach dmw_threshold of local thresholds{
	//use state level changes as our main events
	use ${JT}/historicalminwage-master/exports/mw_state_changes.dta, clear
		//drop irrelevant vars
		drop mw_* source*
		drop if year < 1980 | year > 2020
		drop if statefips == 11
		
		//generate some important variables
		gen event = _n //this is the event number that specifies each of these
		
	//here we want to merge in the minimum wage dataset to calculate the actual mw change amount
	expand 2 , generate(dup)
		bysort event (dup): replace year = year-1 if dup
		
	//create an academic year variable
	gen year_academic = 0
	forval yr = 1978/2021{
			replace year_academic = `yr' if month > 6  & year == `yr'
			replace year_academic = `yr' if month <= 6 & year == `=`yr'+1'
	}
	assert year_academic > 0
		
	merge m:1 year statefips using "${mwdata}/exports/mw_state_annual.dta", keep(match) nogen
		
	bysort event (year) : gen dmw = max_mw - max_mw[_n-1]
		keep if !missing(dmw) //this drops the expanded observations from the year before the change
		drop if dmw == 0 //there are a few rogue minwage changes that we drop here
		
	//we have seven instances where there are two min wage changes in the same year same state 
		//here, we combine them to be one change 
	collapse (firstnm) stateabb (sum) dmw (min) mw, by(statefips year_academic)

	if `switch_pctchng'{
		gen mw_pctchng = dmw/mw
			replace dmw = mw_pctchng
			drop mw*
	}
	if `switch_pctHSwage'{
		merge 1:1 statefips year_academic using `HS_wages', keep(match) nogen
		gen mw_pctchngHS = dmw/uhe
			replace dmw = mw_pctchng
			drop mw* uhe
	}

	//create indicators for whether these are events of interest or smaller minimum wage changes
	gen dmw_interest = (dmw >= `dmw_threshold')
	gen dmw_small = (dmw < `dmw_threshold')
		
	***********************************
	//summary stat figures

	//renumber events
	gsort -dmw_interest
	gen event = _n if dmw_interest
		summ event 
			local numevents = r(max)
			local n`=`dmw_threshold'*1000' = `numevents'
		if `switch_pctchng'{
			local legend 2 "6 percent (`n60')" 4 "8 percent (`n80')" 6 "10 percent (`n101')"
		}
		if `switch_pctHSwage'{
			local legend 2 "2 percent (`n20')" 4 "4 percent (`n40')" 6 "6 percent (`n60')" 
		}
		if `switch_nominal'{
			local legend 2 "25 cents (`n250')" 4 "50 cents (`n500')" 6 "75 cents (`n750')"
		}
				

	//save a version that keeps all events
	preserve
		drop event
		tempfile allchanges
			save `allchanges'
	restore

	//save a version that only keeps the major events
	preserve
		keep if dmw_interest 
			rename year_academic dmw_ay
			rename statefips  dmw_statefips
			rename stateabb  dmw_statename
		tempfile events
			save `events'
	restore

	********************************************************************************
	//minimum wage monthly data

	//prep minwage datasets
	use ${mwdata}/exports/mw_state_monthly.dta, clear

	//separate monthly date out into month and year variables
	gen year = yofd(dofm(monthly_date))
	gen month = month(dofm(monthly_date))
		
	//keep years that are relevant given our enrollment data
	keep if year < 2021 & year > 1979

	//create an academic year variable
	gen year_academic = 0
	forval yr = 1979/2021{
			replace year_academic = `yr' if month > 6  & year == `yr'
			replace year_academic = `yr' if month <= 6 & year == `=`yr'+1'
	}

	assert year_academic > 0
		drop if year_academic == 1979 | year_academic == 2021
		
	//merge in federal change dates
	merge m:1 month year using "${mwdata}/exports/mw_federal_changes.dta", keep(match master) //drops changes that are outside of our panel

	gen dmw_fed = (_merge == 3)
		drop _merge day date fed_mw source

	//collapse to get these at the state by year level instead of monthly 
	collapse (max) min* mean* max* (sum) dmw_fed (firstnm) stateabb, by(statefips year_academic)

	//merge in small and significant change indicators 
	merge 1:1 year_academic statefips using `allchanges', keep(match master) //drops changes that are outside of our panel
		drop _merge
		replace dmw = 0 if missing(dmw)
		replace dmw_interest = 0 if missing(dmw_interest)
		replace dmw_small = 0 if missing(dmw_small)

	//create a never treated indicator for states that never have a change of interest
	bysort statefips: egen t = total(dmw_interest)
		gen never_treat = (t == 0)
		drop t

	********************************************************************************
	//create stacked dataset
		
	//now we have a dataset at the state x ACADEMIC year level with indicators for small, significant, and federal changes
	//merge one copy of this dataset onto each event so that each event has its own dataset 
	forval e = 1/`numevents'{
		preserve
			gen event = `e'
			merge m:1 event using `events', keep(match) nogen
			tempfile event`e'
				save `event`e''
		restore
	}
	forval x = 2/`numevents'{
		use `event1', clear
			append using `event`x''
		tempfile event1
			save `event1', replace
	}

	***********************************
	//create leads and lags

	bysort event (year_academic statefips): gen rel_time = year_academic - dmw_ay
			
	summ rel_time
		local relmin = abs(r(min))
		local relmax = abs(r(max))
		
	gen treated = (statefips == dmw_statefips)

	// leads
	cap drop F_*
	forval x = 1/`relmin' {  // drop the first lead
		gen     F_`x' = rel_time == -`x'
		replace F_`x' = 0 if never_treat==1
		gen     F`x'treat = F_`x'*treated
	}

			
	//lags
	cap drop L_*
	forval x = 0/`relmax' {
		gen     L_`x' = rel_time ==  `x'
		replace L_`x' = 0 if never_treat==1
		gen     L`x'treat = L_`x'*treated
	}

	***********************************
	//create controls
	//controls should be equal to one if these states are acting as a control state in the event, but have an event themselves in the post period
	gen control = (statefips != dmw_statefips)
	bysort event (year_academic) : gen post = (rel_time >= 0)

	gen control_origeventpost = dmw_interest * control * post
	gen control_fedeventpost = dmw_fed * control * post
	gen control_othereventpost = dmw_small * control * post

	tempfile stacked
		save `stacked'

	********************************************************************************
	**ENROLLMENT DATA**
	********************************************************************************
	use "${main}/data/outcomes_enrollment.dta", clear
		
	local type ""
	if `switch_nponly'{
		drop if control == 3
		local type  "_nponly`type'"
	}
	if `switch_fponly'{
		keep if control == 3
		local type  "_fponly`type'"
	}
	if `switch_ftonly'{
		keep if efalevel == 22
		local type  "_FT`type'"
	}
	if `switch_ptonly'{
		keep if efalevel == 42
		local type  "_PT`type'"
	}
	if `switch_pubonly'{
		keep if control == 1
		local type  "_public`type'"
	}
	if `switch_menonly'{
		replace eftotlt = eftotlm
		local type  "_menonly`type'"
	}
	if `switch_womenonly'{
		replace eftotlt = eftotlw
		local type  "_womenonly`type'"
	}
	if `switch_msu'{
		keep if msu
		local type  "_msu`type'"
	}
	if `switch_urban'{
		keep if urban
		local type  "_urban`type'"
	}
	else if `switch_all'{
		local type = "_all"
	}

	//collapse by state
	collapse (sum) eftotlt , by(file_year deg st_fips)
		rename eftotlt eftotl
		drop if st_fips > 56 //this drops all of the US territories
		drop if file_year < 1986 // this drops 1980, 1984 and 1985, for which our data are much spottier
		
	if `switch_controls'{
		//generate indicators for if a state has a free CC program in a given year
		gen free_cc = 0
			replace free_cc = 1 if st_fips == 5  & file_year > 2016
			replace free_cc = 1 if st_fips == 6  & file_year > 2018
			replace free_cc = 1 if st_fips == 9  & file_year > 2019
			replace free_cc = 1 if st_fips == 10 & file_year > 2005
			replace free_cc = 1 if st_fips == 13 & file_year > 2013
			replace free_cc = 1 if st_fips == 15 & file_year > 2017
			replace free_cc = 1 if st_fips == 18 & file_year > 1990
			replace free_cc = 1 if st_fips == 19 & file_year > 2019
			replace free_cc = 1 if st_fips == 20 & file_year > 2016
			replace free_cc = 1 if st_fips == 21 & file_year > 2016
			replace free_cc = 1 if st_fips == 22 & file_year > 2016
			replace free_cc = 1 if st_fips == 24 & file_year > 2016
			replace free_cc = 1 if st_fips == 28 & file_year > 2016
			replace free_cc = 1 if st_fips == 29 & file_year > 2016
			replace free_cc = 1 if st_fips == 30 & file_year > 2016
			replace free_cc = 1 if st_fips == 32 & file_year > 2016
			replace free_cc = 1 if st_fips == 34 & file_year > 2016
			replace free_cc = 1 if st_fips == 36 & file_year > 2016
			replace free_cc = 1 if st_fips == 40 & file_year > 2016
			replace free_cc = 1 if st_fips == 41 & file_year > 2016
			replace free_cc = 1 if st_fips == 44 & file_year > 2016
			replace free_cc = 1 if st_fips == 46 & file_year > 2016
			replace free_cc = 1 if st_fips == 47 & file_year > 2016
			replace free_cc = 1 if st_fips == 53 & file_year > 2016
			replace free_cc = 1 if st_fips == 54 & file_year > 2016
			
		//add controls for unemployment rate by state
		preserve
			use "${main}/data/clean_controls_labormarket.dta", clear
			
			keep statefips unemp_rate year
				rename statefips st_fips
				rename year file_year
			
			tempfile unemployment
			save `unemployment'
		restore 
		
		merge m:1 file_year st_fips using `unemployment', assert(match)	nogen
		
		rename file_year year_academic
		rename st_fips statefips 
		keep year_academic statefips deg eftotl free_cc unemp_rate
		
		local cntrls "_controls"
	}
	//rename vars and reshape if we aren't including controls
	else{	
		rename file_year year_academic
		rename st_fips statefips 
		keep year_academic statefips deg eftotl
	}
			
	reshape wide eftotl , i(year_academic statefips) j(deg)
		gen log_eftotl1 = ln(eftotl1)
		gen log_eftotl3 = ln(eftotl3)
				
	tempfile enroll
	save `enroll'

	local var log_eftotl
	local ytitle "log enrollment in college"

	merge 1:m year_academic statefips using `stacked', keep(match) // keep matches drops years outside of our enrollment panel

	********************************************************************************
	**GRAPHING**
	********************************************************************************

	* define treatment vector
	global treat F*treat L*treat

	*linear combinations of post-reg coeffs
	global lincomm3 F3treat - F1treat
	global lincomm2 F2treat - F1treat
	global lincomm1 F1treat - F1treat
	global lincom0 L0treat - F1treat
	global lincom1 L1treat - F1treat
	global lincom2 L2treat - F1treat
	global lincom3 L3treat - F1treat
	global lincom4 L4treat - F1treat

	if `switch_controls'{
		eststo reg`n':  reghdfe `var'1 $treat free_cc unemp_rate , ///
			a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
			i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips)
				local dof`n' = e(df_r)
				local i = 1
				foreach year in 3 2 1{
					lincom (${lincomm`year'})
						matrix col`n'[`i',1] = r(estimate)
						matrix col`n'[`i++',2] = r(se)
				}
				foreach year in 0 1 2 3 4{
					lincom (${lincom`year'})
						matrix col`n'[`i',1] = r(estimate)
						matrix col`n'[`i++',2] = r(se)
				}
		local n = `n'+1
	}
	else{
		eststo reg`n':  reghdfe `var'1 $treat  , ///
				a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
				i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips)
					local dof`n' = e(df_r)
					local i = 1
					foreach year in 3 2 1{
						lincom (${lincomm`year'})
							matrix col`n'[`i',1] = r(estimate)
							matrix col`n'[`i++',2] = r(se)
					}
					foreach year in 0 1 2 3 4{
						lincom (${lincom`year'})
							matrix col`n'[`i',1] = r(estimate)
							matrix col`n'[`i++',2] = r(se)
					}
		local n = `n'+1
	}
}

	matrix col3[1,3] = -3
	matrix col3[2,3] = -2
	matrix col3[3,3] = -1
	matrix col3[4,3] = 0
	matrix col3[5,3] = 1
	matrix col3[6,3] = 2
	matrix col3[7,3] = 3
	matrix col3[8,3] = 4

	matrix results = col1, col2, col3  //, col4

	clear
	matlist results
	svmat results

	//rename results9  time
	rename results7 time
	rename results1  coeff_1
	rename results2  se_1
	rename results3  coeff_2
	rename results4  se_2
	rename results5  coeff_3
	rename results6  se_3
	//rename results7  coeff_4
	//rename results8  se_4

foreach x in 1 2 3{
	gen ci_upper_`x' = coeff_`x' + invttail(`dof`x'', 0.025)*se_`x'
	gen ci_lower_`x' = coeff_`x' - invttail(`dof`x'', 0.025)*se_`x'
}

//graphing style things
	grstyle init
	grstyle set plain
	grstyle set size 8pt: subheading axis_title

	
twoway rarea ci_upper_1 ci_lower_1 time, color(sienna%40) ||  ///
		line coeff_1 time, lcolor(sienna) mcolor(sienna) || ///
		rarea ci_upper_2 ci_lower_2 time, color(forest_green%40) ||  ///
		line coeff_2 time, lcolor(forest_green) mcolor(forest_green) ||  ///
		rarea ci_upper_3 ci_lower_3 time, color(erose%50) ||  ///
		line coeff_3 time, lcolor(erose) mcolor(erose)   ///
	ylab(-.18(.02).04, labsize(small)) yscale(range(-.12 .04)) yline(0, lp(dash) lc(gs10)) xline(-1, lp(dash) lc(black)) ///
	xlab(-3(1)4, grid labsize(small)) legend(col(3) order("`legend'") size(small)) ///
	ytitle("`ytitle'") xtitle("years since minimum wage change") legend(region(lstyle(none))) 
graph export "${output}/MWlevels3`measure'_`var'_`type'`cntrls'.pdf", replace
	
	

